package com.zsj;

import com.util.SparseArray;
import com.util.readArr;
import org.junit.Test;

import java.io.*;

/**
 * 课后作业
 * @Student 郑声军
 * @teacher 尚硅谷
 * 1在稀疏数组的基础是，将稀疏数组保存到磁盘上，比如map.data
 * 2恢复原来的数组时，读取map.data进行恢复
 *
 *
 * 在工具类已经写好了，将二维数组转换为稀疏数组，及将数组写入磁盘中的方法
 * 测试类进行演示
 */
public class SparseArrayTestIO {

    int [][] sparseArr =null;
    /**
     * 将稀疏数组写入到磁盘中
     */
    @Test
    public void saveArrayFile(){
        //先进行测试
        int [][] arr =new int[8][8];
        sparseArr = SparseArray.SparseOutput(arr);


        BufferedWriter bw = null;
        File file = new File("d:\\sparse.txt");
        try {
            bw = new BufferedWriter(new FileWriter(file));
            if (!file.exists()) {
                file.createNewFile();
            }
            StringBuilder allBuilder = new StringBuilder();
            for (int[] rows : sparseArr) {
                StringBuilder rowBuilder = new StringBuilder();
                for (int item : rows) {
                    rowBuilder.append(item + "\t");
                }
                allBuilder.append(rowBuilder + "\n");
            }
            bw.write(String.valueOf(allBuilder));
            bw.flush();
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 从磁盘中读取稀疏数组并且恢复为二维数组
     */
    @Test
    public void readToFile(){
        int [][] arr =new int[8][8];
        sparseArr = SparseArray.SparseOutput(arr);
        try {
            System.out.println("将稀疏数组保存到磁盘并命名为map.data");
            File f = new File("D:\\sparse.txt");
            FileOutputStream fos = new FileOutputStream(f);
            OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
            System.out.println("写入中----------");
            for(int i = 0; i < sparseArr.length; i++) {
                osw.write(sparseArr[i][0] + "," + sparseArr[i][1] + "," + sparseArr[i][2] + ",");
            }
            osw.close();//关闭输出流
            fos.close();//关闭输出流
            System.out.println("写入磁盘成功");
            //读取磁盘中的map.data文件
            System.out.println("读取中----------");
            FileInputStream fis = new FileInputStream(f);
            InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
            StringBuffer sb = new StringBuffer();
            while(isr.ready()) {
                sb.append((char)isr.read());
            }
            isr.close();//关闭输入流
            fis.close();//关闭输入流
            System.out.println("读取成功");
            //String ss = sb.toString();
            String[] sb1 = sb.toString().split(",");
            //System.out.printf("从磁盘读取的字符串为：\n%s\n", ss);//格式化输出
            //恢复稀疏数组
            int sum1 = 0;
            int[][] sparseArr1 = new int[sb1.length/3][3];
            sparseArr1[0][0] = Integer.parseInt(sb1[0]);
            sparseArr1[0][1] = Integer.parseInt(sb1[1]);
            sparseArr1[0][2] = Integer.parseInt(sb1[2]);
            for(int i = 3; i < sb1.length; i += 3) {
                sum1++;
                sparseArr1[sum1][0] = Integer.parseInt(sb1[i]);
                sparseArr1[sum1][1] = Integer.parseInt(sb1[i+1]);
                sparseArr1[sum1][2] = Integer.parseInt(sb1[i+2]);
            }
            System.out.println("还原后的稀疏数组为：");
            for(int i = 0; i < sparseArr1.length; i++) {
                System.out.printf("%d\t%d\t%d\n", sparseArr1[i][0], sparseArr1[i][1], sparseArr1[i][2]);
            }
            //恢复原始二维数组
            int[][] chessArr3 = new int[sparseArr1[0][0]][sparseArr1[0][1]];
            for(int i = 1; i < sparseArr1.length; i++) {
                chessArr3[sparseArr1[i][0]][sparseArr1[i][1]] = sparseArr1[i][2];
            }
            System.out.println("还原后的二维数组为:");
            for(int[] a : chessArr3) {
                for(int b : a) {
                    System.out.printf("%d\t", b);
                }
                System.out.println();
            }
        }catch(IOException e) {
            e.printStackTrace();
        }
    }
}

